<?php
namespace Shopmobile\Model;
use Think\Model\ViewModel;
/*
 * 三个用户表的对应关系
 * sm_shop_users.userid = sm_user.id
 * event_user.uzuid = sm_shop_users.id
 */
class UserViewModel extends ViewModel{
    public $viewFields = array(
     // 'event_user'=>array('_table'=>"sm_events_users",'id','sid','eventid','uzuid','score','addtime','sumpay','goodrate','refond','likeme','followme','fanscont','shareme','collect','weitao','brandme','weibo_fl','weibo_fs','weibo_fx','newcomer','oldcomer','trade_times','buyer_vip','buyer_lev','buyer_signday','location','_type'=>'LEFT'),
     // 'shop_user'=>array('_table'=>"sm_shop_users",'_on'=>'shop_user.id=event_user.uzuid','id'=>'shop_uid','addtime','firstevent','lastlogin','lastseeds','lotts','wins','seeds','nextseeds','rate_good','rate_neutral','rate_bad','his_trades','trads','his_pay','sumpay','_type'=>'LEFT'),
     'shop_user'=>array('_table'=>"sm_shop_users",'id'=>'shop_uid','sid','addtime','firstevent','lastlogin','lastseeds','lotts','wins','seeds','nextseeds','rate_good','rate_neutral','rate_bad','his_trades','trads','his_pay','sumpay','_type'=>'LEFT'),
     'shop_user_info'=>array('_table'=>"sm_shop_users_info",'_on'=>'shop_user_info.shop_uid=shop_user.id','avatar','friend_link','qrcode_url','short_url','eps_url','channel_id','channel_name','friends','joined_events','win_guesses','win_gifts','_type'=>'LEFT'),
     'sys_user'=>array('_table'=>"sm_user",'_on'=>'sys_user.id=shop_user.userid','id'=>'sys_uid','mixednick','status','_type'=>'LEFT'),
     'true_nick'=>array('_table'=>"sm_user_mixnick",'_on'=>"sys_user.mixednick=true_nick.mixednick",'usernick','_type'=>'LEFT')
	 );
	public $sid;
	public $eid;
	public $uid;
	public $mixednick;
	public $usernick;
	public $user_info;
	public function getUser(){
		$data["sid"] = $this->sid;
		if($this->mixednick){
			$data["mixednick"] = $this->mixednick;
		}
		if($this->usernick){
			$data["usernick"] = $this->usernick;
		}
		$friend = current($this->where($data)->getField("id,usernick,mixednick,sys_uid,shop_uid,avatar,seeds"));
		$friend["sid"]=$this->sid;
		$this->user_info = $friend;
	}
	public function getUserByNick(){
		$data["sid"] = $this->sid;
		$data["usernick"] = $this->usernick;
		$friend = current($this->where($data)->getField("id,usernick,mixednick,sys_uid,shop_uid,avatar,seeds"));
		$id = $friend["id"];
		$friend["sid"]=$this->sid;
		$friend["avatar"] = $friend["avatar"]?$friend["avatar"]:"";
		$this->user_info = $friend;
		return $id;
	}
	public function getUserByMixedNick(){
		$data["sid"] = $this->sid;
		$data["mixednick"] = $this->mixednick;
		$friend = current($this->where($data)->getField("id,usernick,mixednick,sys_uid,shop_uid,avatar,seeds"));
		$id = $friend["id"];
		$friend["sid"]=$this->sid;
		$friend["avatar"] = $friend["avatar"]?$friend["avatar"]:"";
		$this->user_info = $friend;
		return $id;
	}
	public function updateMixNick(){
		$SYSU = M("User");
		$data["id"] = $this->user_id;
		$data["mixednick"] = $this->mixednick;
		$SYSU->save($data);print_r($data);
		echo $SYSU->_sql();
	}
	public function addUser(){
		
		$SMU = D("User");
		$SPU = D("ShopUsers");
		if($this->mixednick){
			$smu["mixednick"] = $this->mixednick;
		}
		if($this->usernick){
			$smu["usernick"] = $this->usernick;
		}
		$sys_uid = $SMU->where($smu)->getField("id");

		if(!$sys_uid){
			$smu["jointime"] = date("Y-m-d H:i:s");
			$sys_uid = $SMU->add($smu);
		}
		$spu["sid"] = $this->sid;
		$spu["userid"] = $sys_uid;
		$shop_uid = $SPU->where($spu)->getField("id");
		if(!$shop_uid){
			$spu["addtime"]=date("Y-m-d H:i:s");
			$spu["firstevent"]=$this->eid;
			$shop_uid=$SPU->add($spu);
			$spu["score"]=0; //数据库中并无此字段，但第一次返回给前端应有该字段。
			
			$USR = M("ShopUsersInfo");
			$sa["shop_uid"]=$shop_uid;
			$sa["sid"]=$this->sid;
			$sa["avatar"]=$this->getAvatar();
			$sa["friend_link"]=C("URL")."/?friend=".urlencode($this->mixednick)."&sid=".$this->sid;
			$USR->add($sa);
		}
		$this->user_info = array_merge($smu,$sa,$spu);
		$this->user_info["seeds"]=0;
		$this->user_info["sys_uid"]=$sys_uid;
		$this->user_info["shop_uid"]=$shop_uid;
		return $shop_uid;
	}
	public function getAvatar(){		
			Vendor("taobaosdk.top.TopClient");
			Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
			Vendor("taobaosdk.top.RequestCheckUtil");
			Vendor("taobaosdk.top.request.UserAvatarGetRequest");
			$c = new \TopClient;
			$c->appkey = C("FT_KEY");//用后台的key
			$c->secretKey = C("FT_SECRET"); 
			$req = new \UserAvatarGetRequest;
			$req->setNick($this->mixednick);
			$resp = $c->execute($req);
			$this->avatar = (string)$resp->avatar;
		return $this->avatar;
	}
	public function getUserFields(){
		$data["shop_user.id"] = $this->uid;
		$data["sid"] = $this->sid;
		$friend = current($this->where($data)->getField($this->myfields));
		$this->user_info = $friend;
		return $friend;
	}
	public function getUserField(){
		$data["shop_user.id"] = $this->uid;
		$data["sid"] = $this->sid;
		return $this->where($data)->getField($this->myfields);
	}
	public function getUserJifen(){
		$data["shop_user.id"] = $this->shop_uid;
		$data["sid"] = $this->sid;
		$SHOPUSER = M("ShopUsers");
		return $SHOPUSER->where($data)->getField("seeds");
	}
	public function decJifen($jifen){
		$data["shop_user.id"] = $this->shop_uid;
		$data["sid"] = $this->sid;
		$SHOPUSER = M("ShopUsers");
		return $SHOPUSER->where($data)->setDec("seeds",$jifen);
	}

}